﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BTS6000
{
    public partial class frmSQL_Search : Form
    {
        public frmSQL_Search(bool isChinese)
        {
            if (isChinese)
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
            }
            else
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
            }

            InitializeComponent();
        }

        private void frmSQL_Search_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.Tools;

            Language.ToCHT(this);

            Kuge.AppSetting.INI.Get("History", this.Controls);

            chkBarcode.Checked = true;
            chkModel.Checked = true;
            chkDatetime.Checked = true;
            chkConclusion.Checked = true;
            chkOCV1.Checked = true;
        }

        private void frmSQL_Search_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;

            Kuge.AppSetting.INI.Set("History", this.Controls);

            e.Cancel = false;
        }

        private ColumnHeader CreateColumnHeader(CheckBox chk)
        {
            ColumnHeader NewHeader;

            NewHeader = new ColumnHeader { Name = string.Format("_{0}", chk.Name), Text = chk.Text };
            NewHeader.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
 
            return NewHeader;
        }

        private ColumnHeader CreateColumnHeader(string s)
        {
            ColumnHeader NewHeader;

            NewHeader = new ColumnHeader { Name = string.Format("_{0}", s), Text = s };
            NewHeader.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
 
            return NewHeader;
        }

        private string MakeColumns()
        {
            StringBuilder sb = new StringBuilder();
            
            lstOutput.Columns.Clear();
            lstOutput.Items.Clear();

            if(chkBarcode.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkBarcode));

                sb.Append("[Barcode],");
            }

            if (chkModel.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkModel));

                sb.Append("[Model],");
            }

            if (chkDatetime.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkDatetime));

                sb.Append("[Datetime],");
            }

            if (chkConclusion.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkConclusion));

                sb.Append("[Conclusion],");
            }

            if (chkOCV1.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkOCV1));

                sb.Append("[OCV1],");
            }

            if (chkIR.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkIR));

                sb.Append("[IR],");
            }

            if (chkR1.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkR1));

                sb.Append("[R1],");

                if (chkR1_T.Checked)
                {
                    lstOutput.Columns.Add(CreateColumnHeader(chkR1_T));

                    sb.Append("[R1T],");
                }
            }

            if (chkR2.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkR2));

                sb.Append("[R2],");
            }

            if (chkDSG.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkDSG));

                sb.Append("[DSGV],");

                if (chkDSG_C.Checked)
                {
                    lstOutput.Columns.Add(CreateColumnHeader(chkDSG_C));

                    sb.Append("[DSGC],");
                }
            }

            if (chkDOT.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkDOT));
                sb.Append("[DOT],");

                if (chkDOC.Checked)
                {
                    lstOutput.Columns.Add(CreateColumnHeader(chkDOC));
                    sb.Append("[DOC],");
                }
            }

            if (chkDOT1.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkDOT1));

                sb.Append("[DOT1],");
            }

            if (chkCHG.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkCHG));

                sb.Append("[CHGV],");

                if (chkCHG_C.Checked)
                {
                    lstOutput.Columns.Add(CreateColumnHeader(chkCHG_C));

                    sb.Append("[CHGC],");
                }
            }

            if (chkCOT.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkCOT));

                sb.Append("[COT],");

                if (chkCOC.Checked)
                {
                    lstOutput.Columns.Add(CreateColumnHeader(chkCOC));

                    sb.Append("[COC],");
                }
            }

            if (chkShort.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkShort));
                sb.Append("[ST],");

                if (chkShortV.Checked)
                {
                    lstOutput.Columns.Add(CreateColumnHeader(chkShortV));

                    sb.Append("[SV],");
                }

                if (chkShortRV.Checked)
                {
                    lstOutput.Columns.Add(CreateColumnHeader(chkShortRV));

                    sb.Append("[SRV],");
                }
            }

            if (chkCell.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkCell));

                sb.Append("[CELL_VOL],");
            }

            if (chkOCV2.Checked)
            {
                lstOutput.Columns.Add(CreateColumnHeader(chkOCV2));

                sb.Append("[OCV2],");
            }

            sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }

        private void btnSearchByDate_Click(object sender, EventArgs e)
        {
            DateTime f, t;
            if (!DateTime.TryParse(txtFrom.Text, out f))
            {
                MessageBox.Show("Datetime is invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFrom.Focus();
                txtFrom.SelectAll();
                return;
            }
            if (!DateTime.TryParse(txtTo.Text, out t))
            {
                MessageBox.Show("Datetime is invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTo.Focus();
                txtTo.SelectAll();
                return;
            }

            if(BTS6000.SQL.Instance==null)
            {
                MessageBox.Show("No SQL Connection!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string columns, conditions;
            DataTable table;

            this.Cursor = Cursors.WaitCursor;

            columns = MakeColumns();

            if(rbtnPassOnly.Checked)
            {
                conditions = string.Format("([Datetime] BETWEEN '{0}' AND '{1}') AND ([Conclusion]='PASSED')", txtFrom.Text, txtTo.Text);
            }
            else if(rbtnFailOnly.Checked)
            {
                conditions = string.Format("([Datetime] BETWEEN '{0}' AND '{1}') AND ([Conclusion]='FAILED')", txtFrom.Text, txtTo.Text);
            }
            else
            {
                conditions = string.Format("[Datetime] BETWEEN '{0}' AND '{1}'", txtFrom.Text, txtTo.Text);
            }

            table = BTS6000.SQL.Instance.Select("[TP_6000_Plus]", columns, conditions);

            dgv.DataSource = table;

            if(table!=null)
            {
                foreach (DataRow row in table.Rows)
                {
                    ListViewItem item = new ListViewItem();

                    item.Text = string.Format("{0}", row[0]);

                    for (int i=1;i<table.Columns.Count;i++)
                    {
                        ListViewItem.ListViewSubItem subItem = new ListViewItem.ListViewSubItem();
                        subItem.Text = string.Format("{0}", row[i]);

                        item.SubItems.Add(subItem);
                    }

                    lstOutput.Items.Add(item);
                }
            }

            this.Cursor = Cursors.Default;
        }

        #region 快捷功能

        private void lnkToday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt);
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now.AddDays(-1);

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt);
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkTheDayBeforeYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now.AddDays(-2);

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt);
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkFromYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt.AddDays(-1));
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkFromTheDayBeforeYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt.AddDays(-2));
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }





        #endregion

        #region HTML

        private void btnExport_Click(object sender, EventArgs e)
        {
            Kuge.Report.HTML.Export(lstOutput, "Data");
        }


        #endregion

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            DataTable table;

            table = BTS6000.SQL.Instance.Select("TP_6000_Plus", " * ", "[Conclusion]='FAILED' or [Conclusion]='PASSED'");

            dgv.DataSource = table;
        }
    }
}
